package com.baitengsoft.patentapimonitor.api.service.impl;

import cn.hutool.core.io.file.FileReader;
import com.baitengsoft.patentapimonitor.api.dao.FileMonitorReportDao;
import com.baitengsoft.patentapimonitor.api.entity.FileMonitorReportEntity;
import com.baitengsoft.patentapimonitor.api.service.FileMonitorReportService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author Chen 2018/2/5
 */
@Service
public class FileMonitorReportServiceImpl implements FileMonitorReportService {
    @Autowired
    private FileMonitorReportDao fileMonitorReportDao;
    @Autowired
    private WebExcelExportService webExcelExportService;

    @Override
    public List<FileMonitorReportEntity> queryList(Integer platformid, String reportName) {
        Example example = new Example(FileMonitorReportEntity.class);
        example.setOrderByClause(" CreateDate desc");

        Example.Criteria criteria = example.createCriteria();
        PageHelper.startPage(1,10);
        criteria.andEqualTo("platformid",platformid);
        if (StringUtils.isNotBlank(reportName)){
            criteria.andLike("title","%"+reportName+"%");
        }
        return fileMonitorReportDao.selectByExample(example);
    }

    @Override
    public FileMonitorReportEntity selectByDate(Integer platformid,String today) {
        Example example = new Example(FileMonitorReportEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("platformid",platformid);
        criteria.andLike("createdate",today+"%");
        List<FileMonitorReportEntity> fileMonitorReportEntities = fileMonitorReportDao.selectByExample(example);
        if (fileMonitorReportEntities==null || fileMonitorReportEntities.size()<1)
            return null;
        return fileMonitorReportEntities.get(0);
    }

    @Async
    @Override
    public void createFile(Integer platformid) {
        try {
            String url = webExcelExportService.exportAnalysisReport(platformid);
            FileMonitorReportEntity entity = new FileMonitorReportEntity();
            entity.setFileurl(url);
            entity.setPlatformid(platformid);
            entity.setTitle(DateUtils.dateformat(new Date())+"-统计报表.xls");
            entity.setCreatedate(new Date());
            fileMonitorReportDao.insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public FileMonitorReportEntity selectByFileurl(Integer platformid, String fileurl) {
        Example example = new Example(FileMonitorReportEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("platformid",platformid);
        criteria.andEqualTo("fileurl",fileurl);
        List<FileMonitorReportEntity> fileMonitorReportEntities = fileMonitorReportDao.selectByExample(example);
        if (fileMonitorReportEntities==null || fileMonitorReportEntities.size()<1)
            return null;
        return fileMonitorReportEntities.get(0);
    }

    @Override
    public Res exportReportFile(Integer platformid,FileMonitorReportEntity entity, HttpServletResponse response) {
        String fileName = entity.getTitle();
        try {
            ServletOutputStream os = response.getOutputStream();
            File file = new File(WebExcelUtils.getNewFilePath(platformid,entity.getFileurl())+".xls");
            fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            FileReader reader = new FileReader(file,"UTF-8");
            reader.writeToStream(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            return Res.error("下载失败");
        }
        return Res.ok();
    }
}
